package cn.myzqu.kafka.demo;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author guoqiujing
 * @date 2020/11/25 11:34
 */
public class ProducerFastStart {


    private static final String brokerList = "172.168.50.171:9092";
    private static final String topic = "topic-demo";

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("bootstrap.servers", brokerList);
        // 配置生成者客户端参数并创建KafkaProducer实例
        KafkaProducer producer = new KafkaProducer(properties);
        // 构建需要发送的消息
        ProducerRecord record = new ProducerRecord(topic, "hello,Kafka!");
        // 发送消息
        try {
            producer.send(record);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();
        }
    }

}
